package com.android.geotrack;

import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class TopTracksParser extends DefaultHandler
{
	private LinkedList<Track> topTracks = new LinkedList<Track>();
	private Boolean tagId = false;
	private Boolean tagTitle = false;
	private Boolean tagArtist = false;
	private Boolean tagRank = false;
	private Boolean tagThumbnail_medium = false;
	private Boolean tagThumbnail_large = false;
	private Track t;

	/**
	* Gets called, whenever a new tag is found.
	*/
	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes)
		throws SAXException
	{
		if (localName.equals("Track")) {
			t = new Track();
		}
		
		if (localName.equals("Id")) {
			tagId = true;
		}
		
		if (localName.equals("Title")) {
			tagTitle = true;
		}
		
		if (localName.equals("Artist")) {
			tagArtist = true;
		}
		
		if (localName.equals("Rank")) {
			tagRank = true;
		}
		
		if (localName.equals("Medium")) {
			tagThumbnail_medium = true;
		}
		
		if (localName.equals("Large")) {
			tagThumbnail_large = true;
		}
	}

	/**
	* Gets called, whenever a closed tag is found.
	*/
	@Override
	public void endElement(String uri, String localName, String name)
		throws SAXException
	{
		if (localName.equals("Track")) {
			topTracks.add(t);
		}
		
		if (localName.equals("Id")) {
			tagId = false;
		}
		
		if (localName.equals("Title")) {
			tagTitle = false;
		}
		
		if (localName.equals("Artist")) {
			tagArtist = false;
		}
		
		if (localName.equals("Rank")) {
			tagRank = false;
		}
		
		if (localName.equals("Medium")) {
			tagThumbnail_medium = false;
		}
		
		if (localName.equals("Large")) {
			tagThumbnail_large = false;
		}
	}

	/**
	* Gets called whenever text is found. For example between <Title> and </Title>
	*/
	@Override
	public void characters(char[] ch, int start, int length)
		throws SAXException
	{
		String s = new String(ch,start,length).trim();
		
		if (tagId == true) {
			t.setId(Integer.parseInt(s));
		}
		
		if (tagTitle == true) {
			t.setTitle(s);
		}
		
		if (tagArtist == true) {
			t.setArtist(s);
		}
		
		if (tagRank == true) {
			t.setRank(Integer.parseInt(s));
		}
		
		if (tagThumbnail_medium == true) {
			t.setThumbnail_medium(s);
		}
		
		if (tagThumbnail_large == true) {
			t.setThumbnail_large(s);
		}
	}

	/**
	* Parses a XML with SAX.
	* Call getParseResult() to get the result.
	*
	* @param xml XML-Code that should be parsed.
	*/
	public void parseXml(String xml)
	{
		try {
			
			// New SAX parser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser;
			saxParser = factory.newSAXParser();
		
			// Parse xml file with give XML
			saxParser.parse(new InputSource(new StringReader(xml)), this);
	
		} catch (ParserConfigurationException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		} catch (SAXException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		} catch (IOException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		}
	}

	/**
	* Returns the result of xml parsing.
	* Call this after calling parseXML().
	*
	* @return ArrayList
	*/
	public LinkedList<Track> getParseResult() {
		return topTracks;
	}
}